$(function () {
    // 设置验证码点击切换
    function setCaptcha() {
        $('#changeImg img').click((event) => {
            $(event.target).attr('src', '/captcha?' + Math.random())
        })
    }
    setCaptcha()

    // 登录逻辑
    $("#tologin").click(e => {
        e.preventDefault()
        var formData = {
            author: $('#authorname').val(),
            password: $('#password').val(),
            imgcode: $('#imgcode').val()
        }
        // console.log(formData);
        $.ajax({
            url: '/login',
            type: 'post',
            data: formData,
            dataType: 'json',
            success: (d) => {
                console.log("success");
                switch (d.code) {
                    case 0:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 1:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 2:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 3:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 4:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 5:
                        swal.fire({ title: d.msg, type: "success", confirmButtonText: '进入主页' }).then(() => {
                            window.location.href = "/"
                        })
                        break;
                    case 6:
                        swal.fire({ title: d.msg, type: "success", confirmButtonText: 'OK' }).then(() => {
                            window.location.href = "/manageauthors"
                        })
                        break;
                }
            },
            error: () => {
                swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                    window.location.href = "/login"
                })
            }
        })
    })

    // 重置密码----发送邮件
    $("#send").click(e => {
        e.preventDefault()
        var email = $('#email')
        var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/g;//邮箱验证

        var countDownTime = parseInt(60);
        function countDown(countDownTime) {
            var timer = setInterval(function () {
                if (countDownTime >= 0) {
                    $("#send").val(countDownTime + 's');
                    $("#send").prop('disabled', true)
                    $("#send").attr("class", "send btn-success col-md-3 col-sm-3 col-xs-3")
                    countDownTime--;
                } else {
                    clearInterval(timer);
                    $("#send").val('sent');
                    $("#send").prop('disabled', false)
                }
            }, 1000);
        }
        if (reg.test(email.val())) {
            email.css("border-color", "green");
            $("#send").css("background-color", "#5cb85c")
            countDown(countDownTime);
            $.ajax({
                url: '/sendemail',
                type: 'post',
                data: { email: email.val(), author: $('#username').val() },
                dataType: 'json',
                success: (d) => {
                    console.log("success");
                },
                error: () => {
                    swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                        window.location.href = "/login"
                    })
                }
            })
        } else {
            email.css("border-color", "red");
            $("#send").css("background-color", "#c9302c")
        }
    })

    // 注册账号----发送邮件
    $("#s_send").click(e => {
        e.preventDefault()
        var email = $('#s_email')
        var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/g;//邮箱验证

        var countDownTime = parseInt(60);
        function countDown(countDownTime) {
            var timer = setInterval(function () {
                if (countDownTime >= 0) {
                    $("#s_send").val(countDownTime + 's');
                    $("#s_send").prop('disabled', true)
                    $("#s_send").attr("class", "send btn-success col-md-3 col-sm-3 col-xs-3")
                    countDownTime--;
                } else {
                    clearInterval(timer);
                    $("#s_send").val('sent');
                    $("#s_send").prop('disabled', false)
                }
            }, 1000);
        }
        if (reg.test(email.val())) {
            email.css("border-color", "green");
            $("#s_send").css("background-color", "#5cb85c")
            countDown(countDownTime);
            $.ajax({
                url: '/sendemail',
                type: 'post',
                data: { email: email.val(), author: $('#s_username').val() },
                dataType: 'json',
                success: (d) => {
                    console.log("success");
                },
                error: () => {
                    swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                        window.location.href = "/login"
                    })
                }
            })
        } else {
            email.css("border-color", "red");
            $("#s_send").css("background-color", "#c9302c")
        }
    })

    // 重置密码逻辑
    $("#resetpwd").click(e => {
        e.preventDefault()
        var formData = {
            author: $('#username').val(),
            yzm: $('#yzm').val(),
            pwd: $('#pwd').val(),
            email: $('#email').val()
        }
        console.log(formData);
        $.ajax({
            url: '/resetpwd',
            type: 'post',
            data: formData,
            dataType: 'json',
            success: (d) => {
                // console.log("success");
                switch (d.code) {
                    case 0:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 1:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 2:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 3:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 4:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 5:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 6:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 7:
                        swal.fire({ title: d.msg, type: "success", confirmButtonText: '马上登录' }).then(() => {
                            window.location.href = "/login"
                        })
                        break;
                }
            },
            error: () => {
                swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                    window.location.href = "/login"
                })
            }
        })
    })

    // 注册逻辑
    $("#tosignIn").click(e => {
        e.preventDefault()
        var formData = {
            author: $('#s_username').val(),
            email: $('#s_email').val(),
            yzm: $('#s_yzm').val(),
            pwd1: $('#s_pwd').val(),
            pwd2: $('#s_pwd2').val(),
        }
        console.log(formData);
        $.ajax({
            url: '/signin',
            type: 'post',
            data: formData,
            dataType: 'json',
            success: (d) => {
                switch (d.code) {
                    case 0:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 1:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 2:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 3:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 4:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 5:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 6:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 7:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 8:
                        swal.fire({ title: d.msg, type: "success", confirmButtonText: '马上登录' }).then(() => {
                            window.location.href = "/login"
                        })
                        break;
                }
            },
            error: () => {
                swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                    window.location.href = "/login"
                })
            }
        })
    })
})